package com.zensun.web.controller.system;

import com.zensun.common.annotation.Log;
import com.zensun.common.core.controller.BaseController;
import com.zensun.common.core.domain.BaseEntity;
import com.zensun.common.core.domain.R;
import com.zensun.common.enums.BusinessType;
import com.zensun.system.domain.SysDept;
import com.zensun.system.domain.dto.SysDeptDto;
import com.zensun.system.domain.dto.SysDeptExcludeDto;
import com.zensun.system.domain.vo.SysDeptDetailVo;
import com.zensun.system.domain.vo.SysDeptTreeListVo;
import com.zensun.system.domain.vo.SysDeptTreeVo;
import com.zensun.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门信息
 *
 * @author gmk
 */
@Api(value = "部门管理", tags = "部门模块")
@RestController
@RequestMapping("/system/dept")
public class SysDeptController extends BaseController {
    @Autowired
    private ISysDeptService deptService;

    /**
     * 获取部门列表
     */
    @ApiOperation("查询部门列表树")
    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @GetMapping("/list")
    public R<List<SysDeptTreeListVo>> list(SysDeptDto sysDeptDto) {
        return R.success(deptService.selectDepTreeList(sysDeptDto));
    }

    /**
     * 查询部门下拉框树
     */
    @ApiOperation("查询部门下拉框树")
    @GetMapping("/list/exclude")
    public R<List<SysDeptTreeVo>> excludeChild(SysDeptExcludeDto sysDeptExcludeDto) {
        return R.success(deptService.excludeChild(sysDeptExcludeDto));
    }

    /**
     * 根据部门编号获取详细信息
     */
    @ApiOperation("根据部门ID获取详细信息")
    @PreAuthorize("@ss.hasPermi('system:dept:query')")
    @GetMapping(value = "/{deptId}")
    public R<SysDeptDetailVo> getInfo(@PathVariable Long deptId) {
        return R.success(deptService.selectDeptById(deptId));
    }

    /**
     * 新增部门
     */
    @ApiOperation("新增部门")
    @PreAuthorize("@ss.hasPermi('system:dept:add')")
    @Log(title = "部门管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@Validated(BaseEntity.AddGrop.class) @RequestBody SysDept dept) {
        return R.toAjax(deptService.insertDept(dept));
    }

    /**
     * 修改部门
     */
    @ApiOperation("修改部门")
    @PreAuthorize("@ss.hasPermi('system:dept:edit')")
    @Log(title = "部门管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@Validated(BaseEntity.UpdateGrop.class) @RequestBody SysDept dept) {
        return R.toAjax(deptService.updateDept(dept));
    }

    /**
     * 删除部门
     */
    @ApiOperation("删除部门")
    @PreAuthorize("@ss.hasPermi('system:dept:remove')")
    @Log(title = "部门管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptId}")
    public R remove(@ApiParam("单个ID") @PathVariable Long deptId) {
        return R.toAjax(deptService.deleteDeptById(deptId));
    }
}
